package com.lin.benchmade.timer;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lin.benchmade.utils.ConfigReader;

public class InportTimer {
	private final Logger LOG = LoggerFactory.getLogger(InportTimer.class);
	private Connection dbConnection = null;
	private int minid;
	private int maxid;
	private final Map<String, Integer> count_map = new HashMap<String, Integer>();
	private final Statement stat;

	public InportTimer() throws Exception{
		Class.forName("com.mysql.jdbc.Driver");
		dbConnection = DriverManager.getConnection(ConfigReader.getEntry("dburl"), ConfigReader.getEntry("username"),
				ConfigReader.getEntry("password"));
		stat = dbConnection.createStatement();
		ResultSet res = stat.executeQuery(("SELECT MIN(novelid) from novel"));
		if (res.next()) {
			minid = res.getInt(1) - 1;
		}
		res = stat.executeQuery(("SELECT MAX(novelid) from novel"));
		if (res.next()) {
			maxid = res.getInt(1);
		}
		res.close();
		LOG.info("dbinfo: " + minid + "\t" + maxid);
	}

	public Map<String, Integer> build() throws Exception {
		String sql = "select n.name name, a.name author, pv.gpv gpv from novel n left join author a on n.authorid=a.authorid LEFT JOIN t_novelid_indexed pv ON n.novelid=pv.novelid where n.novelid between ? and ?";
		PreparedStatement ps = dbConnection.prepareStatement(sql);
		int pv = 0;
		String name, author;
		int num = 0;
		while (minid < maxid) {
			ps.setInt(1, minid + 1);
			minid = minid + 100000;
			ps.setInt(2, minid);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				num++;
				if (num % 100000 == 0) {
					LOG.info("Cursor to :" + num);
				}
				pv = rs.getInt("gpv");
				if (pv > 1) {
					name = rs.getString("name");
					author = rs.getString("author");
					if (name != null && !name.trim().equals("")) {
						pv = count_map.get(name) == null ? pv : Math.max(count_map.get(name), pv);
						count_map.put(name, pv);
					}
					if (pv > 0 && author != null && !author.trim().equals("")) {
						pv = count_map.get(author) == null ? pv : Math.max(count_map.get(author), pv);
						count_map.put(author, pv);
					}
				}
			}
			rs.close();
		}
		return count_map;
	}

	public void close() {
		try {
			dbConnection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		InportTimer it = new InportTimer();
		Map<String, Integer> map = it.build();
		System.out.println(map.size());
		it.close();
		FileWriter fw2 = new FileWriter("d:/pv2.txt");
		BufferedWriter bw = new BufferedWriter(fw2);

		for (Entry<String, Integer> entry : map.entrySet()) {
			bw.write(entry.getKey() + "\t" + entry.getValue());
			bw.newLine();
		}

		bw.close();
		fw2.close();
	}

}
